package com.aop.listener;

import cn.hutool.core.util.ObjectUtil;
import com.aop.domain.TestInterface;
import com.aop.event.InterfaceEvent;
import com.aop.service.TestInterfaceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * @Description:
 * @author: zxl
 * @Data:2021/3/24
 */
//异步执行
@Async
@Component
public class InterfaceListener {
    @Resource
    private TestInterfaceService testInterfaceService;

    @EventListener(InterfaceEvent.class)
    public void recordCount(InterfaceEvent event) {
        TestInterface test = (TestInterface) event.getSource();
        QueryWrapper<TestInterface> qw = new QueryWrapper<>();
        qw.eq("interface_url", test.getInterfaceUrl());
        TestInterface one = testInterfaceService.getOne(qw);
        //存在则更新调用次数,不存在则添加记录
        if (ObjectUtil.isNotNull(one)) {
            Integer count = one.getCount() + 1;
            one.setCount(count);
            testInterfaceService.updateById(one);
        } else {
            //saveOrUpdate方法存在则更新,不存在则插入
            testInterfaceService.save(test);
        }

    }
}
